package com.project.decoration.controller.cases;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationCase;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationCaseService;
import com.project.decoration.service.DecorationHouseTypeService;
import com.project.decoration.service.DecorationStyleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 案例controller
 * @date 2025/02/13 01:49
 */
@Controller
@ResponseBody
@RequestMapping("case")
public class DecorationCaseController {

    @Autowired
    private DecorationCaseService decorationCaseService;

    /** 分页获取案例 */
    @PostMapping("getDecorationCasePage")
    public Result getDecorationCasePage(@RequestBody DecorationCase decorationCase) {
        Page<DecorationCase> page = new Page<>(decorationCase.getPageNumber(),decorationCase.getPageSize());
        QueryWrapper<DecorationCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationCase.getName()),DecorationCase::getName,decorationCase.getName())
                .eq(StringUtils.isNotBlank(decorationCase.getType()),DecorationCase::getType,decorationCase.getType())
                .eq(StringUtils.isNotBlank(decorationCase.getStyle()),DecorationCase::getStyle,decorationCase.getStyle())
                .like(StringUtils.isNotBlank(decorationCase.getCity()),DecorationCase::getCity,decorationCase.getCity())
                .like(StringUtils.isNotBlank(decorationCase.getCommunity()),DecorationCase::getCommunity,decorationCase.getCommunity());
        Page<DecorationCase> decorationCasePage = decorationCaseService.page(page, queryWrapper);
        return Result.success(decorationCasePage);
    }

    @GetMapping("getDecorationCaseIndex")
    public Result getDecorationCaseIndex() {
        QueryWrapper<DecorationCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(DecorationCase::getCreateTime).last("limit 2");
        List<DecorationCase> caseList = decorationCaseService.list(queryWrapper);
        return Result.success(caseList);
    }

    @GetMapping("getDecorationCaseSix")
    public Result getDecorationCaseSix() {
        QueryWrapper<DecorationCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(DecorationCase::getCreateTime).last("limit 6");
        List<DecorationCase> caseList = decorationCaseService.list(queryWrapper);
        return Result.success(caseList);
    }

    /** 根据id获取案例 */
    @GetMapping("getDecorationCaseById")
    public Result getDecorationCaseById(@RequestParam("id")String id) {
        DecorationCase decorationCase = decorationCaseService.getById(id);
        return Result.success(decorationCase);
    }

    /** 保存案例 */
    @PostMapping("saveDecorationCase")
    public Result saveDecorationCase(@RequestBody DecorationCase decorationCase) {
        boolean save = decorationCaseService.save(decorationCase);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑案例 */
    @PostMapping("editDecorationCase")
    public Result editDecorationCase(@RequestBody DecorationCase decorationCase) {
        boolean save = decorationCaseService.updateById(decorationCase);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除案例 */
    @GetMapping("removeDecorationCase")
    public Result removeDecorationCase(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationCaseService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("案例id不能为空！");
        }
    }

}
